package org.qf.web;



import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.qf.entity.SmbmsBill;
import org.qf.entity.SmbmsProvider;
import org.qf.service.SmbmsBillService;

import java.util.List;

@Slf4j
@Controller
public class SmbmsBillController {
    @Autowired
    SmbmsBillService smbmsBillService;

    /**
     * 列出账单
     * @param currPage
     * @param isPayment
     * @param providerId
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/showBillList")
    @PostMapping
    public String showProvideList(@RequestParam(defaultValue = "1") int currPage, @RequestParam(defaultValue = "0") int isPayment, @RequestParam(defaultValue = "0")int providerId, String name, Model model) {
        SmbmsBill smbmsBill = new SmbmsBill();
        smbmsBill.setProductName(name);
        smbmsBill.setIsPayment(isPayment);
        smbmsBill.setProviderId(providerId);
        List<SmbmsProvider>  providers = smbmsBillService.ListProvider();
        PageInfo<SmbmsBill> pageInfo = smbmsBillService.showBillList(currPage,smbmsBill);
        if (pageInfo.getList().size() > 0) {
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("name", name);
            model.addAttribute("isPayment", isPayment);
            model.addAttribute("providerId", providerId);
            model.addAttribute("providers", providers);
            return "billList";
        } else {
            return "error";
        }
    }
    /**
     * 点击跳转账单页面
     *
     * @return
     */
    @RequestMapping("/addBill")
    public String addBill(Model model) {
        List<SmbmsProvider>  providers = smbmsBillService.ListProvider();
        if (providers.size() > 0){
            model.addAttribute("providers", providers);
            return "billAdd";
        }else{
            return "error";
        }

    }

    /**
     * 添加账单
     * @param smbmsBill
     * @return
     */
    @RequestMapping("/addBill1")
    public String addBill1(SmbmsBill smbmsBill) {
        log.info("smbmsProvide:{}", smbmsBill);
        int i = smbmsBillService.addBill(smbmsBill);
        if (i > 0) {
            return "redirect:/showBillList";
        } else {
            return "redirect:/addBill";
        }
    }

    /**
     * 通过id查找账单信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/viewBill")
    public String viewUser(int id, Model model) {
        SmbmsBill smbmsBill = smbmsBillService.showBillById(id);
        List<SmbmsProvider> providers = smbmsBillService.ListProvider();
        SmbmsProvider p = null;
        for (SmbmsProvider provider : providers) {
            if (provider.getId() == smbmsBill.getProviderId()){
                p = provider;
            }
        }
        if (smbmsBill != null) {
            model.addAttribute("p", p);
            model.addAttribute("b", smbmsBill);
            return "billView";
        } else {
            return "error";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/updateBillView")
    public String updateBillView(int id, Model model) {
        SmbmsBill smbmsBill = smbmsBillService.showBillById(id);
        List<SmbmsProvider> providers = smbmsBillService.ListProvider();
        SmbmsProvider p = null;
        for (SmbmsProvider provider : providers) {
            if (provider.getId() == smbmsBill.getProviderId()){
                p = provider;
            }
        }
        if (smbmsBill != null) {
            model.addAttribute("p", p);
            model.addAttribute("b", smbmsBill);
            model.addAttribute("providers", providers);
            return "billUpdate";
        } else {
            return "error";
        }
    }

    /**
     * 修改
     *
     * @param smbmsBill
     * @return
     */
    @RequestMapping("/updateBill")
    public String updateBill(SmbmsBill smbmsBill) {
        log.info("smbmsBill:{}",smbmsBill);
        int i = smbmsBillService.updateBill(smbmsBill);
        if (i > 0) {
            return "redirect:/showBillList";
        } else {
            return "error";
        }
    }

    /**
     * 删除账单
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteBill")
    public String deleteBill(int id) {
        int i = smbmsBillService.deleteBill(id);
        if (i > 0) {
            return "redirect:/showBillList";
        } else {
            return "error";
        }
    }
}
